// 导入http模块
const http = require("http");

// 创建http服务对象
const server = http.createServer((request, response) => {
  response.end(`<!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Document</title>
      <style>
        table,
        td {
          border: 1px solid black;
          border-collapse: collapse;
        }
        table tr:nth-child(odd) {
          background-color: #aef;
        }
        table tr:nth-child(even) {
          background-color: #fcb;
        }
        td {
          width: 180px;
          height: 50px;
        }
      </style>
    </head>
    <body>
      <table>
        <tr>
          <td></td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
        </tr>
        <tr>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </table>
    </body>
    <script>
      // 获取所有的td
      let tds = document.querySelectorAll("td");
      tds.forEach((td) => {
        td.onclick = function () {
          this.style.background = "red";
        };
      });
    </script>
  </html>
  `);
});

// 监听端口 并启动服务
server.listen(9000, () => {
  console.log("服务启动成功");
});
